package main

import (
	"encoding/json"
	"fmt"
)

type Addr struct {
	Province string
	City     string
}
type Stu struct {
	Name string `json:"name"`
	Age  int    `json:"-"` // 表示不参与序列化
	Addr Addr
}

func main() {

	var map01 = map[int]string{1: "111", 2: "222", 3: "333"}
	var stu01 = Stu{Name: "yuan", Age: 18, Addr: Addr{Province: "Hebei", City: "beijing"}}

	// 序列化
	jsonMap01, _ := json.Marshal(map01)
	jsonStu01, _ := json.Marshal(stu01)

	fmt.Println(string(jsonMap01))
	fmt.Println(string(jsonStu01))

	// 反序列化
	// var x  = make(map[int]string)
	var map02 map[int]string
	json.Unmarshal([]byte(jsonMap01), &map02)
	fmt.Println("map02", map02)

	// var data = `{"name":"yuan","Age":18,"Addr":{"Province":"Hebei","City":"beijing"}}`
	var stu02 Stu

	json.Unmarshal([]byte(jsonStu01), &stu02)
	fmt.Println(stu02)
	fmt.Println(stu02.Name)
	fmt.Println(stu02.Addr.City)

}
